package com.atfusu.controller;

import com.atfusu.base.BaseController;
import com.atfusu.bean.entity.Admin;
import com.atfusu.service.AdminService;
import com.atfusu.service.RoleService;
import com.atfusu.util.QiniuUtil;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

/**
 * Title:
 * Author: fusu
 * DateTime:  2022/11/26
 * Description:
 **/
@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {

    private static final String PAGE_INDEX = "admin/index";
    private static final String PAGE_CREATE = "admin/create";
    private static final String PAGE_EDIT = "admin/edit";
    private static final String PAGE_SUCCESS = "common/success";
    private static final String PAGE_UPLOAD = "admin/upload";

    @DubboReference
    private AdminService adminService;

    @DubboReference
    private RoleService roleService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @RequestMapping
    public String findPage(HttpServletRequest request, Map map) {
        //获取查询条件及分页
        Map<String, Object> filters = getFilters(request);
        //根据条件及分页规矩查询
        PageInfo page = adminService.findPage(filters);
        //放入请求域
        map.put("page", page);
        map.put("filters", filters);
        return PAGE_INDEX;
    }

    @RequestMapping("/create")
    public String create() {
        return PAGE_CREATE;
    }

    @RequestMapping("/save")
    public String save(Admin admin) {
        admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        adminService.insert(admin);
        return PAGE_SUCCESS;
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Map map) {
        //查询id用户
        Admin admin = adminService.getById(id);
        map.put("admin", admin);
        return PAGE_EDIT;
    }

    @RequestMapping("/update")
    public String update(Admin admin) {
        adminService.update(admin);
        return PAGE_SUCCESS;
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id) {
        adminService.delete(id);
        return "redirect:/admin";
    }

    @RequestMapping("/uploadShow/{adminId}")
    public String uploadShow(@PathVariable Long adminId, Map map) {

        map.put("adminId", adminId);
        return PAGE_UPLOAD;
    }

    @RequestMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file, Long adminId) throws IOException {
        //上传头像到七牛云
        String fileName = UUID.randomUUID().toString();
        QiniuUtil.upload2Qiniu(file.getBytes(), fileName);
        //将记录添加到数据库内
        Admin admin = new Admin();
        admin.setId(adminId);
        admin.setHeadUrl("http://rm5dtfln6.hn-bkt.clouddn.com/" + fileName);
        adminService.update(admin);
        return PAGE_SUCCESS;
    }

    @RequestMapping("/assignShow/{adminId}")
    public String assignShow(@PathVariable Long adminId, Map map) {
        //noAssignRoleList
        //assignRoleList
        Map<String, Object> roleByAdminId = roleService.findRoleByAdminId(adminId);
        map.put("adminId", adminId);
        //将roleByAdminId集合中的所有数据以key,value形式加入到map集合
        map.putAll(roleByAdminId);
        return "admin/assignShow";
    }

    @RequestMapping("/assignRole")
    public String assignRole(Long adminId, Long[] roleIds) {
        //将角色与admin用户的对应关系存储到数据库内
        roleService.saveAdminRoleRelationship(adminId, roleIds);

        return "common/success";
    }
}
